<template>
  <div>
    <div class="excelright" >
      <el-button type="primary">导入excel表
       <input type="file" @change="importf(this)" accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"/>
      </el-button>




      <!-- 导出excel文件 -->
      <el-button type="primary">
        <download-excel
          type="primary"
          class="export-excel-wrapper"
          :data="json_data"
          :fields="json_fields"
          name="filename.xls"
        >
          导出excel表
        </download-excel>
      </el-button>
    </div>
  </div>
</template>

<script>
export default {
  name: '',
  props:{},
  data () {
    return {
           json_fields: {
        "工号": "jobnumber",    //常规字段
        "员工姓名（必填）": "name",   
        "手机号（必填）": "phone.mobile", //支持嵌套属性
        "部门": "department", //支持嵌套属性
        "职位": "post", //支持嵌套属性
        // "备用电话": {
        //   field: "phone.landline",
        //             //自定义回调函数
        //   callback: value => {
        //     return `Landline Phone - ${value}`;
        //   }
        // }
      },
      json_data: [
        {
          name: "魏佳",
          jobnumber: "2020001",
          post: "前端工程师",
          department: "开发部",
          phone: {
            mobile: "15701793476",
            landline: "13281357561"
          }
        },
      ],
      json_meta: [
        [
          {
            " key ": " charset ",
            " value ": " utf- 8 "
          }
        ]
      ]
    }
  },
  components: {},
  methods: {
    // handleSelectedFile (convertedData) {
    //   console.log(convertedData)
    // }
     importf(obj) {
      let _this = this;  
        this.file = event.currentTarget.files[0];  
        var rABS = false; //是否将文件读取为二进制字符串  
       var file = this.file; 
 
        FileReader.prototype.readAsBinaryString = function(f) {  
          var binary = "";  
         var rABS = false; //是否将文件读取为二进制字符串  
         var pt = this;  
         var workbook; //读取完成的数据  
12         // var excelData;  
13         var reader = new FileReader(); 
14         reader.onprogress = function(e) { 
15           let total = file.size;
16           _this.progress = (e.loaded/total)*100;
17           console.log( _this.progress);
18         };   
19         reader.onload = function(e) {
20           try {
21             var bytes = new Uint8Array(reader.result);  
22             var length = bytes.byteLength;  
23             for(var i = 0; i < length; i++) {  
24               binary += String.fromCharCode(bytes[i]);  
25             }  
26             if(rABS) {  
27               workbook = XLSX.read(btoa(fixdata(binary)), { //手动转化  
28                 type: 'base64'  
29               });  
30             }else {  
31               workbook = XLSX.read(binary, {  
32                 type: 'binary'  
33               });  
34             } 
35             // excelData = []; 
36           } catch(e) {
37             console.log('文件类型不正确');
38             return;
39           }
40           var fromTo = '';
41           for (var sheet in workbook.Sheets) {
42             if (workbook.Sheets.hasOwnProperty(sheet)) {
43               fromTo = workbook.Sheets[sheet]['!ref'];
44               _this.excelData = _this.excelData.concat(XLSX.utils.sheet_to_json(workbook.Sheets[sheet]));
45             }
46           }
47           console.log(_this.excelData);
48         };
49 
50         reader.readAsArrayBuffer(f);  
51 
52       } 
53  
54       var reader = new FileReader();
55       if(rABS) {  
56         reader.readAsArrayBuffer(file);  
57       }else {  
58         reader.readAsBinaryString(file);  
59       } 
60 
61       
62     },
  },
  mounted() {},
  computed: {},
  watch: {}
}
</script>

<style lang='scss' scoped>
// .excelright{
//   margin-left: 1000px;
// }
</style>